
import { ESCesiumViewer } from "@czmSrc/ESCesiumViewer";
import { EngineObject, ESSceneObject, ESVisualObject } from "earthsdk3";
import { createProcessingFromAsyncFunc, step } from "xbsj-base";
import { BaseEditingWrapper, TranslationEditingWrapper } from "./wrapper";
export function createEditingSystemProcessing() {
    const process = createProcessingFromAsyncFunc(async (cancelsManager, czmViewer: ESCesiumViewer, sceneObject: ESVisualObject, modes: string[] | string, useKeyToSwitch: boolean = true) => {
        const editingModes = Array.isArray(modes) ? [...modes] : [modes];
        czmViewer.editingEvent.emit({ objectIDs: [sceneObject.id], type: 'start', add: { modes: editingModes } });
        const clear = () => {
            czmViewer.notSupportEditingCount = 0;
            //@ts-ignore
            czmViewer.editingEvent.emit({ objectIDs: [sceneObject.id], type: 'end', add: { modes: editingModes } });
            process.isRunning && process.cancel();
        };
        cancelsManager.disposer.ad(clear);
        // 监听销毁事件，当对象被销毁时清除编辑模式
        cancelsManager.disposer.ad(sceneObject.toDestroyEvent.don(clear));
        // 监听键盘事件，按下Esc键时清除编辑模式
        cancelsManager.disposer.ad(czmViewer.keyDownEvent.don((e) => {
            e.code == 'Escape' && clear();
        }))
        let currentIndex = 0;
        do {
            const element = editingModes[currentIndex % editingModes.length]
            const over = await step(cancelsManager, async cancelsManager => {
                const d = cancelsManager.disposer;
                const wrapper = EngineObject.contextEditing.getEngineObjConstructor(element, czmViewer.typeName);
                if (!wrapper || !sceneObject.supportEditingModes().includes(element)) {
                    console.log(`未找到编辑模式或不支持当前编辑模式${element},已跳过当前编辑模式，请检查输入`);
                    czmViewer.editingEvent.emit({ objectIDs: [sceneObject.id], type: 'changedFailed', add: { cMode: element, cIndex: currentIndex } });
                    czmViewer.notSupportEditingCount++;
                    return false;
                }
                const currentEditing = new wrapper(sceneObject, czmViewer) as BaseEditingWrapper;
                d.ad(() => {
                    !currentEditing.isDestroyed() && currentEditing.destroy()
                })
                if (currentEditing.supportEditing) {
                    czmViewer.editingEvent.emit({ objectIDs: [sceneObject.id], type: 'changed', add: { cMode: element, cIndex: currentIndex } });
                    czmViewer.notSupportEditingCount = 0;
                } else {
                    console.log(`未找到编辑模式或不支持当前编辑模式${element},已跳过当前编辑模式，请检查输入`);
                    czmViewer.editingEvent.emit({ objectIDs: [sceneObject.id], type: 'changedFailed', add: { cMode: element, cIndex: currentIndex } });
                    czmViewer.notSupportEditingCount++;
                    return false;
                }
                const result = await cancelsManager.promise(new Promise<boolean>(resolve => {
                    if (currentEditing.isDestroyed()) {
                        resolve(false);
                    }
                    d.ad(currentEditing.toDestroyEvent.don(() => {
                        console.log(`编辑模式${element}结束`);
                        resolve(false);
                    }))
                    if (useKeyToSwitch) {
                        d.ad(czmViewer.keyDownEvent.don((e) => {
                            if (e.code == 'Space') {
                                console.log(`编辑模式${element}结束`);
                                resolve(false);
                            }
                        }))
                        d.ad(czmViewer.dblclickEvent.don(() => resolve(true)))
                    }
                }))
                return result;
            })
            if ((useKeyToSwitch && over) ||
                (!useKeyToSwitch && currentIndex == editingModes.length - 1) ||
                czmViewer.notSupportEditingCount == editingModes.length
            ) break;
            currentIndex++;
        } while (true);
    })
    return process;
};
export function createMoveObjectsProcessing() {
    const process = createProcessingFromAsyncFunc(async (cancelsManager, czmViewer: ESCesiumViewer, sceneObjects: ESSceneObject[]) => {
        czmViewer.editingEvent.emit({ objectIDs: sceneObjects.map(o => o.id), type: 'start' });
        const clear = () => {
            //@ts-ignore
            czmViewer.editingEvent.emit({ objectIDs: sceneObjects.map(o => o.id), type: 'end' });
            process.isRunning && process.cancel();
        }
        cancelsManager.disposer.ad(clear); // 监听取消事件，取消时清除编辑模式
        // 监听键盘事件，按下Esc键时清除编辑模式
        cancelsManager.disposer.ad(czmViewer.keyDownEvent.don((e) => {
            e.code == 'Escape' && clear();
        }))
        const currentEditing = new TranslationEditingWrapper(sceneObjects, czmViewer)
        cancelsManager.disposer.ad(() => {
            !currentEditing.isDestroyed() && currentEditing.destroy();
        })
        await cancelsManager.promise(new Promise<void>(resolve => {
            currentEditing.isDestroyed() && resolve();
            cancelsManager.disposer.ad(currentEditing.toDestroyEvent.don(() => {
                console.log(`移动结束`);
                resolve();
            }))
            cancelsManager.disposer.ad(czmViewer.dblclickEvent.don(() => resolve()))
        }))
    })
    return process;
};